﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;

namespace SMS_API_SENDER
{
    public partial class Default : System.Web.UI.Page
    {

        private const String LIST_MESSAGES_URL = @"http://smsgateway.me/api/v3/messages";
        private const String SEND_MESSAGE_URL = @"http://smsgateway.me/api/v3/messages/send";
        private const String LIST_DEVICES_URL = @"http://smsgateway.me/api/v3/devices";

        private const String QUERY_EMAIL_PATTERN = "email={0}&";
        private const String QUERY_PASSWORD_PATTERN = "password={0}&";
        private const String QUERY_DEVICE_PATTERN = "device={0}&";
        private const String QUERY_NUMBER_PATTERN = "number={0}&";
        private const String QUERY_MESSAGE_PATTERN = "message={0}&";

//        email	YES	Your username for the site
//password	YES	Your password for the site
//device	YES	The ID of device you wish to send the message from
//number	YES	The number to send the message to
//message	YES	The content of the message to be sent
//send_at	NO	Time to send the message in Unix Time format
//expires_at	NO	Time to give up trying to send the message at in Unix Time format


        public List<String> Commands
        {
            get
            {
                List<String> tmpList = new List<string>();
                XmlDocument document = new XmlDocument();
                document.Load(Server.MapPath(@"~\commands.xml"));
                CommandObjectFactory.Create(document.SelectNodes("/Root/Commands/CommandItem"));
                foreach (XmlNode node in document.SelectNodes("/Root/Commands/Command"))
                {
                    tmpList.Add(node.InnerText);
                }
                return tmpList;
            }
        }

        public List<CommandObject> CommandObjects
        {
            get
            {
                XmlDocument document = new XmlDocument();
                document.Load(Server.MapPath(@"~\commands.xml"));
                var comList = CommandObjectFactory.Create(document.SelectNodes("/Root/CommandItem"));
                return comList;
            }
        }

        protected void selectCommandCB_Click(object sender, EventArgs e)
        {
            ErrorLbl.Visible = false;
            HtmlInputButton btn = (HtmlInputButton)sender;
            RepeaterItem item = (RepeaterItem)btn.NamingContainer;
            //Label lblCampCode = (Label)item.FindControl("commandLbl");
            HtmlInputText commandTb = (HtmlInputText)item.FindControl("commandTB");
            String command = commandTb.Value;
            CallService(command);
        }

        //protected void Page_Init(object sender, EventArgs e)
        //{
        //    XmlDocument document = new XmlDocument();
        //    document.Load(Server.MapPath(@"~\commands.xml"));
        //    var comList = CommandObjectFactory.Create(document.SelectNodes("/Root/CommandItem"));
        //}

        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    var j =
        //        "{'success':true,'result':{'success':[],'fails':[{'email':'anders.baisgaard@gmail.com','password':'7100Vejle','device':'8888','number':'20686182','message':'CCCCCCCCC','errors':{'device':['The selected device is invalid.']}}]}}";
        //    var jr = JsonConvert.DeserializeObject(j, typeof(ResponseObject));
        //}

        //protected void Page_PreRender(object sender, EventArgs e)
        //{
        //}

        private void CallService(String command)
        {
            try
            {

                String email = userName.Value;
                String pw = password.Value;
                //String deviceId = deviceNumber.Value;
                //String deviceId = DeviceSelectList.SelectedItem.Value;
                String deviceId = deviceIdTb.Value;
                String mobileNumber = targetNumber.Value;
                WebClient wc = new WebClient();
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0}/?", SEND_MESSAGE_URL);
                sb.AppendFormat(QUERY_EMAIL_PATTERN, email);
                sb.AppendFormat(QUERY_PASSWORD_PATTERN, pw);
                sb.AppendFormat(QUERY_DEVICE_PATTERN, deviceId);
                sb.AppendFormat(QUERY_NUMBER_PATTERN, mobileNumber);
                sb.AppendFormat(QUERY_MESSAGE_PATTERN, Server.UrlEncode(command));

                String response = wc.DownloadString(sb.ToString());
                responseHid.Value = response;
                


                //var s = wc.DownloadString(LIST_DEVICES_URL + @"/?email=anders.baisgaard@gmail.com&password=7100Vejle&device=8239&number=004520686182&message=testing");
                //var s = wc.DownloadString(SEND_MESSAGE_URL + @"/?email=anders.baisgaard@gmail.com&password=7100Vejle&device=8239&number=20643645&message=sendt fra Baisgaard kode");

                //var list = JsonConvert.DeserializeObject(s);

            }
            catch (Exception ex)
            {
                ErrorLbl.Visible = true;
                ErrorLbl.Text = ex.Message;
            }
        }

        protected void commandRepeater_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //commandRepeater.DataSource = Commands;
                commandRepeater.DataSource = CommandObjects;
                commandRepeater.DataBind();
            }
        }
    }
}